import java.util.Scanner;

/**
 * Created by IntelliJ IDEA.
 * User: zm
 * Date: 2023/5/14
 */
public class Demo34 {
    static int N = 100010, idx = 0;
    static int[][] song = new int[N][26];
    static int[] cnt = new int[N];
    static char[] str = new char[N];
    public static void insert(char[]  str){
        int p = 0;
        for(int i = 0 ; i < str.length; i++){
            int u = str[i] - 'a';//将字符装换成数字
            if(song[p][u] == 0)song[p][u] = ++idx;
            p = song[p][u];
        }
        cnt[p]++;
    }
    public static int query(char[] str){
        int p = 0;
        for(int i = 0; i < str.length; i++){
            int u = str[i] - 'a';
            if(song[p][u] == 0)return 0;
            p = song[p][u];
        }
        return cnt[p];
    }
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        String sss = scan.nextLine();
        while(n -- > 0){
            String s = scan.nextLine();//直接从该行读数
            String[] st = s.split(" ");
            String s1 = st[0];
            String s2 = st[1];
            if(s1.equals("I")){
                insert(s2.toCharArray());
            }else{
                System.out.println(query(s2.toCharArray()));
            }
        }

    }
}
